/*******************************************************************************
 * Copyright(c) 2023 Levetop Semiconductor Co.,Led. All rights reserved.
 * @file     w25qxx.h
 * @author   UartTFT Application Team
 * @version  V0.0.1
 * @date     2023-01-01
 * @brief
 ********************************************************************************/

#ifndef _W25QXX_H
#define _W25QXX_H

#include "type.h"
#include "ssi_reg.h"

//      W25Q80DV,ID 0xEF13, capacity is 8M-bit��16 Block��256 Sector��4096 Page
//      W25Q16  ,ID 0xEF14, capacity is 16M-Bit��32 Block��512 Sector��8192 Page
//      W25Q32  ,ID 0xEF15, capacity is 32M-Bit��64 Block��1024 Sector��16384 Page
//      W25Q64  ,ID 0xEF16, capacity is 64M-Bit��128 Block��2048 Sector��32768 Page
//      W25Q128 ,ID 0xEF17, capacity is 128M-Bit��256 Block��4096 Sector��65536 Page
//      W25Q256 ,ID 0xEF18, capacity is 256M-Bit��512 Block��8192 Sector��131072 Page
#define NorPageSize (256)            // one Page = 256B (Program)
#define NorSectorSize (256 * 16)     // one Sector = 4KB (Erase)
#define NorBlockSize (256 * 16 * 16) // one Block = 64KB (Erase)

#define Flash_SS_RESET __asm("nop")
#define Flash_SS_SET __asm("nop")
void Flash_SS(uint8_t bitval);

extern uint8_t Flash_Type;
extern uint16_t Nand_page;
extern uint16_t Flash_ID;
extern uint32_t g_sys_clk;

//----------------------------------------------------------------Instruction---------------------------------------------------------------------------
#define W25X_WRITE_ENABLE     0x06
#define W25X_WRITE_ENABLE_VSR 0x50
#define W25X_WRITE_DISABLE    0x04
#define W25X_READ_SR1  0x05
#define W25X_READ_SR2  0x35
#define W25X_READ_SR3  0x15
#define W25X_WRITE_SR1 0x01
#define W25X_WRITE_SR2 0x31
#define W25X_WRITE_SR3 0x11
#define W25X_READ           0x03
#define W25X_READ_FAST      0x0B
#define W25X_READ_FAST_DUAL 0x3B
#define W25X_READ_DUAL_IO   0xBB
#define W25X_PAGE_PROGRAM   0x02
#define W25X_SECTOR_ERASE   0x20
#define W25X_32KBLOCK_ERASE 0x52
#define W25X_64KBLOCK_ERASE 0xD8
#define W25X_CHIP_ERASE     0xC7
#define W25X_POWERDOWN      0xB9
#define W25X_RELEASE_POWERDOWN 0xAB
#define W25X_ENTER_4BYTE_ADDR  0xB7
#define W25X_EXIT_4BYTE_ADDR   0xE9
#define W25X_DEVICE_ID         0xAB
#define W25X_MANUFACTDEVICE_ID 0x90
#define W25X_JEDECDEVICE_ID    0x9F

/*************************************************************** winbond NorFlash **********************************************************************/
// status register
uint8_t W25QXX_ReadSR(uint8_t SRx);                     // read SR
void W25QXX_WriteSR(uint8_t SRx, uint8_t val); 		// write SR
void W25QXX_Wait_Busy(void);                            // wait busy
void W25QXX_Write_Enable(void);                         // write enable
void W25QXX_Write_EnableVolatile(void);                 // write enable(volatile)
void W25QXX_Write_Disable(void);                        // write disable
void W25QXX_4ByteAddress_Mode(void);                    // 4 Byte addr mode
void W25QXX_QE_Enable(void);                         // QE mode
uint32_t W25QXX_ReadID(void);                           // read ID

// erase
void W25QXX_SectorErase4K(uint32_t addr);  // 4K  (erase/Program)
void W25QXX_BlockErase32KB(uint32_t addr); // 32K (erase)
void W25QXX_BlockErase64KB(uint32_t addr); // 64K (erase)
void W25QXX_FullChipErase(void);

// Init
void W25QXX_Init(void);

//-------------------------------------STD_Mode---------------------------------------------------------------
// STD_WriteRead_Init
void SSI1_STD_Init(SSI_TypeDef *SSIx);

// STD_Read
void W25QXX_STD_Read(uint8_t *buf, uint32_t addr, uint16_t num);
// STD_Read_DMA
void W25QXX_STD_Read_DMA(uint8_t *buf, uint32_t addr, uint16_t num);

// STD_Write
void W25QXX_STD_Write_Page(uint8_t *buf, uint32_t addr, uint16_t num);
void W25QXX_STD_Write_NoCheck(uint8_t *buf, uint32_t addr, uint16_t num);
// STD_Write_DMA
void W25QXX_STD_Write_Page_DMA(uint8_t *buf, uint32_t addr, uint16_t num);
void W25QXX_STD_Write_NoCheck_DMA(uint8_t *buf, uint32_t addr, uint16_t num);

//-------------------------------------Dual_Mode---------------------------------------------------------------
// Dual_WriteRead_Init
void NorFlash_Dual_Init(uint8_t command_bits, uint8_t dummy_bits, uint32_t num);

// Dual_Read
void NorFlash_Read(uint8_t *buf, uint32_t addr, uint32_t num);
// Dual_Read_DMA
void NorFlash_DMA_Read(uint8_t *buf, uint32_t addr, uint32_t num);
void NorFlash_DMA_Wait(void);

void PY_STD_UIDRead(uint8_t *buf, uint16_t num);
void PY_STD_OTPRead(uint8_t *buf, uint16_t num);
//-------------------------------------Quad_Mode---------------------------------------------------------------
// none

#endif /* _W25QXX_H */
